/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ui;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JPanel;

/**
 *
 * @author Acer
 */
public class ImprimirRecibo implements Printable{
private javax.swing.JPanel panel1;

    public ImprimirRecibo(JPanel panel1) {
        this.panel1 = panel1;
    }

public void imprimir(){
 try {
            PrinterJob job = PrinterJob.getPrinterJob();
            job.setPrintable(this);
            if(job.printDialog()){
                job.print();
            }            
        } catch (PrinterException ex) {
            Logger.getLogger(ImprimirRecibo.class.getName()).log(Level.SEVERE, null, ex);
        }
    
    }
    @Override
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {

          if (pageIndex > 0) { /* We have only one page, and 'page' is zero-based */
            return NO_SUCH_PAGE;
        }

        Graphics2D g2d = (Graphics2D)graphics;
        g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
        panel1.printAll(graphics);
        return PAGE_EXISTS;

    }
    
}
